package com.nbsaas.boot.shopArticle.rest.resource;

import com.nbsaas.boot.shopArticle.api.apis.ShopArticleCatalogApi;
import com.nbsaas.boot.shopArticle.data.entity.ShopArticleCatalog;
import com.nbsaas.boot.shopArticle.api.domain.request.ShopArticleCatalogRequest;
import com.nbsaas.boot.shopArticle.api.domain.response.ShopArticleCatalogResponse;
import com.nbsaas.boot.shopArticle.api.domain.simple.ShopArticleCatalogSimple;
import com.nbsaas.boot.shopArticle.rest.convert.ShopArticleCatalogSimpleConvert;
import com.nbsaas.boot.shopArticle.rest.convert.ShopArticleCatalogEntityConvert;
import com.nbsaas.boot.shopArticle.rest.convert.ShopArticleCatalogResponseConvert;
import com.nbsaas.boot.shopArticle.data.repository.ShopArticleCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.shopArticle.api.domain.request.ShopArticleCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   资讯分类-业务接口实现
*/
@Transactional
@Service
public class ShopArticleCatalogResource extends BaseResource<ShopArticleCatalog,ShopArticleCatalogResponse, ShopArticleCatalogSimple, ShopArticleCatalogRequest>  implements ShopArticleCatalogApi {

    @Resource
    private ShopArticleCatalogRepository shopArticleCatalogRepository;

    @Override
    public JpaRepositoryImplementation<ShopArticleCatalog, Serializable> getJpaRepository() {
        return shopArticleCatalogRepository;
    }

    @Override
    public Function<ShopArticleCatalog, ShopArticleCatalogSimple> getConvertSimple() {
        return new ShopArticleCatalogSimpleConvert();
    }

    @Override
    public Function<ShopArticleCatalogRequest, ShopArticleCatalog> getConvertForm() {
        return new ShopArticleCatalogEntityConvert();
    }

    @Override
    public Function<ShopArticleCatalog, ShopArticleCatalogResponse> getConvertResponse() {
        return new ShopArticleCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<ShopArticleCatalogSimple> list(PageRequest request) {
        ShopArticleCatalogSimpleConvert convert=new ShopArticleCatalogSimpleConvert();
        if (request instanceof ShopArticleCatalogSearch){
             ShopArticleCatalogSearch searchRequest=(ShopArticleCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<ShopArticleCatalogSimple> root(ShopArticleCatalogSearch search) {
        ListResponse<ShopArticleCatalogSimple> result=new  ListResponse<>();
        List<ShopArticleCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


